package com.yuan.yblog.security.pojo;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.yuan.yblog.crud.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serial;
import java.io.Serializable;
import java.util.*;

/**
 * 在线登录信息体
 * 可用于Security上下文对象存储的信息
 */
@Data
//JsonIgnoreProperties忽略类中不存在的字段，否则redis会报序列化错误
@JsonIgnoreProperties(ignoreUnknown = true)
@AllArgsConstructor
@NoArgsConstructor
public class LoginUser implements Serializable, UserDetails {
    @Serial
    private static final long serialVersionUID = -1L;
    /**
     * 用户信息
     */
    private User user;

    /**
     * 用户权限
     */
    private Collection<SimpleGrantedAuthority> authorities;

    /**
     * 登录ip
     */
    private String ip;

    /**
     * 登录token
     */
    private String token;


    public LoginUser(User user, Collection<SimpleGrantedAuthority> authorities) {
        this.user = user;
        this.authorities = authorities;
    }

    @Override
    public Collection<SimpleGrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getEmail();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
